#include "kernel.h"

unsigned int checksum(unsigned short *buf, int nbytes)
{
        unsigned int sum;
        unsigned short oddbyte;

        sum = 0;
        while (nbytes > 1) {
                sum += *buf++;
                nbytes -= 2;
        }

        if (nbytes == 1) {
                oddbyte = 0;
                *((unsigned short *) &oddbyte) = *(unsigned short *) buf;
                sum += oddbyte;
        }

        sum = (sum >> 16) + (sum & 0xffff);
        sum += (sum >> 16);


        return (unsigned short) ~sum;
}

